package com.example.f9demo;

import android.app.Activity;
import android.util.Log;

public class F9function {
	UsbController sUsbController = null;	
	 private static char findHex(byte b) {
		  int t = new Byte(b).intValue();
		  t = t < 0 ? t + 16 : t;
		  if ((0 <= t) && (t <= 9)) {
		   return (char) (t + '0');
		  }
		  return (char) (t - 10 + 'A');
		 }
	 private static String byteToString(byte b) {
		  byte high, low;
		  byte maskHigh = (byte) 0xf0;
		  byte maskLow = 0x0f;
		  high = (byte) ((b & maskHigh) >> 4);
		  low = (byte) (b & maskLow);
		  StringBuffer buf = new StringBuffer();
		  buf.append(findHex(high));
		  buf.append(findHex(low));
		  return buf.toString();
		 }
	 private byte[] Reverse(byte[] b) {

	        byte[] temp = new byte[b.length];
	        for (int i = 0; i < b.length; i++) {
	            temp[i] = b[b.length - 1 - i];
	        }
	        return temp;
	    }
	 private short bytetoshort(byte[] b) {

	        b = Reverse(b);
	        short mask = 0xff;
	        short temp = 0;
	        short n = 0;
	        for (int i = 0; i < 4; i++) {
	            n <<= 8;
	            temp = (short)(b[i] & mask);
	            n |= temp;
	        }
	        return n;
	    }
	public F9function(Activity parentActivity)
	{
	
	}
	private  short rf_countnum(short[]pData,int datalength)
	{
		short tmpcount;
		int i;
		tmpcount = 0;
		for(i = 0; i< datalength;i++)
		{
			tmpcount =(short) (tmpcount +  (short)pData[i]);
		} 
		
		Log.i("DEBUG tmpcount is", String.valueOf(tmpcount));
	  return tmpcount;
	}
	private void countResult(byte[]tmp,int DataLength)
	{
		short tmpcount;
		int i;
		short[] tmpdata = new short[32]; 
		for(i = 0;i < 32;i++)
		{
			
				tmpdata[i] = (short)(tmp[i] & 0x0FF);
		}

		tmpcount = rf_countnum(tmpdata,30);
      
		 tmp[30] = (byte)(tmpcount % 256);//(byte)(tmpcount & 0x00FF);
		 tmp[31] = (byte)(tmpcount /256);//((tmpcount & 0xFF00) >> 8);
		
	}
	/*private short countResultend(byte[]tmp,byte DataLength)
	{
		short tmpcount;
		//byte[] redata = new byte[2];
		tmpcount = rf_countnum(tmp,DataLength);
		Log.i("DEBUG OUT tmpcount is",String.valueOf(tmpcount));
		 return tmpcount;
		
	}
	*/
	private  byte[] F9comand(Activity parentActivity, byte[]sndata, byte snddatalen)
	{
		final int VID = 0x0416;//0x2341;
		final int PID = 0x501B;//0x0001;//I believe it is 0x0000 for the Arduino Megas
		byte[] redata = new byte[32];
	
	

		if(sUsbController == null){
			//Log.i("DEBUG>>>>>>++++", "run  here");
			sUsbController = new UsbController(parentActivity, VID, PID,sndata,1);
			sUsbController.stop();
			redata = sUsbController.receive();
		}else{
			sUsbController.stop();
			sUsbController = new UsbController(parentActivity, VID, PID,sndata,1);
	
			sUsbController.stop();
			redata = sUsbController.receive();
		}
		return redata;
	}
	public long rf_loadkey_hex(Activity parentActivity, byte mode, byte secnr, byte[] kkey)
	{	    


        byte[] sndData = new byte[32];
        byte[] recData = new byte[32];

		
		///
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'L';
		sndData[3] = 'D';
		sndData[4] = 'K';
		sndData[5] = 0x05;
		sndData[6] = mode;
		sndData[7] = (byte)13;
		System.arraycopy(kkey, 0, sndData, 8, 6);
		///
		//countResult((Data_Struct*)&keyClass);//����У���
		countResult(sndData,(byte)32);

		recData = F9comand( parentActivity,sndData,(byte)32);

		System.arraycopy(recData, 0, sndData, 0, 32);
		countResult(sndData,(byte)32);
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
			return 3; //У��ʹ���
		if((recData[0] == 'O') & (recData[1] == 'K'))
			return 0;
		else if((recData[0] == 'E') && (recData[1] == 'R'))
			return ((long)recData[2]);
	    else
			return 1;

	}
	public byte[] rf_card(Activity parentActivity, byte mode)
	{
		
		 byte[] byData = new byte[4];
        byte[] sndData = new byte[32];
        byte[] recData = new byte[32];
        //short tmpData;

		byte i;

	    
	    byData[0] = 0x00;
	    byData[1] = 0x00;
	    byData[2] = 0x00;
	    byData[3] = 0x00;
		///
	
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'F';
		sndData[3] = 'N';
		sndData[4] = 'C';
		sndData[5] = 0x05;
		
		sndData[6] = mode;
		
		countResult(sndData,(byte)32);
		
		recData = F9comand( parentActivity,sndData,(byte)32);
		//Log.i("DEBUG OUTPUT recData[0]",String.valueOf((int)recData[30]));
		// Log.i("DEBUG OUTPUT recData[1]",String.valueOf((int)recData[31]));
		System.arraycopy(recData, 0, sndData, 0, 30);
	     countResult(sndData,(byte)30);
		 
		 
		 String str = new String();
		 str = "";
		 for(i = 0; i< 32;i++)
		  str = str + byteToString(recData[i]);
		 Log.i("DEBUG OUTPUT recData",str);
		 String str1 = new String();
		 str1 = "";
		 for(i = 0; i< 32;i++)
			  str1 = str1 + byteToString(sndData[i]);
		 Log.i("DEBUG OUTPUT sndData",str1);
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
		{
			Log.i("DEBUG>>>>>>++++", "У��ʹ���");
			//System.arraycopy(recData, 2, byData, 0, 4);
			
			return byData; //У��ʹ���
		}
		if((recData[0] == 'O') & (recData[1] == 'K'))
		{
			Log.i("DEBUG>>>>>>++++", "��������");
			System.arraycopy(recData, 2, byData, 0, 4);
			return byData;
		}
		else if((recData[0] == 'E') && (recData[1] == 'R'))
		{
			Log.i("DEBUG>>>>>>++++", "���ش���");
			return byData;
		}
		else
		{
			Log.i("DEBUG>>>>>>++++", "����ȷ�Ĵ���");
			return byData;
		}
		//byData[0] = recData[2];
		//return byData;
	}
	public long rf_authentication(Activity parentActivity, byte mode, byte secnr)
	{
		byte[] byData = new byte[4];
        byte[] sndData = new byte[32];
        byte[] recData = new byte[32];
       

	    byData[0] = 0x00;
	    byData[1] = 0x00;
	    byData[2] = 0x00;
	    byData[3] = 0x00;
		///
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'A';
		sndData[3] = 'U';
		sndData[4] = 'T';
		sndData[5] = 0x05;
		sndData[6] = mode;
		sndData[7] = secnr;		
		sndData[8] = 1;//0 ������ 1 skj
		countResult(sndData,(byte)32);

		recData = F9comand( parentActivity,sndData,(byte)32);

		System.arraycopy(recData, 0, sndData, 0, 32);
		countResult(sndData,(byte)32);
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
			return 3; //У��ʹ���
		if((recData[0] == 'O') & (recData[1] == 'K'))
			return 0;
		else if((recData[0] == 'E') && (recData[1] == 'R'))
			return 1;
		else
			return 2;
	}
	public byte[]rf_read_hex(Activity parentActivity, byte addr)
	{
		byte[] byData = new byte[16];
        byte[] sndData = new byte[32];
        byte[] recData = new byte[32];
	
		int Count;
	    byData[0] = 0x00;
	    byData[1] = 0x00;
	    byData[2] = 0x00;
	    byData[3] = 0x00;
		///
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'R';
		sndData[3] = 'C';
		sndData[4] = 'D';
		sndData[5] = 0x05;
		sndData[6] = addr;
		
		countResult(sndData,(byte)32);

		recData = F9comand( parentActivity,sndData,(byte)32);

		System.arraycopy(recData, 0, sndData, 0, 32);
		countResult(sndData,(byte)32);
		for(Count = 0; Count < 16;Count++)
		{
			byData[Count] = 0;
		}
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
			return byData; //У��ʹ���
		if((recData[0] == 'O') & (recData[1] == 'K'))

		{
			System.arraycopy(recData, 2, byData, 0, 16);
			return byData;
		}
		else if((recData[0] == 'E') && (recData[1] == 'R'))
			return byData;
		else
			return byData;
	}
	public long rf_write_hex(Activity parentActivity, byte addr, byte[] wdata)
	{
		byte[] sndData = new byte[32];
        byte[] recData = new byte[32];

	 
		///
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'W';
		sndData[3] = 'R';
		sndData[4] = 'D';
		sndData[5] = 0x05;
		sndData[6] = addr;
		System.arraycopy(wdata, 0, sndData, 7, 16);
		countResult(sndData,(byte)32);

		recData = F9comand( parentActivity,sndData,(byte)32);

		System.arraycopy(recData, 0, sndData, 0, 32);
		countResult(sndData,(byte)32);
	
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
			return 3; //У��ʹ���
		if((recData[0] == 'O') & (recData[1] == 'K'))
            return 0;
		else if((recData[0] == 'E') && (recData[1] == 'R'))
			return 1;
		else
			return 1;	
	}
	public long rf_halt(Activity parentActivity)
	{
		byte[] sndData = new byte[32];
        byte[] recData = new byte[32];
	
		///
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'H';
		sndData[3] = 'L';
		sndData[4] = 'T';
		sndData[5] = 0x05;
		
		
		countResult(sndData,(byte)32);

		recData = F9comand( parentActivity,sndData,(byte)32);

		System.arraycopy(recData, 0, sndData, 0, 32);
		countResult(sndData,(byte)32);
	
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
			return 3; //У��ʹ���
		if((recData[0] == 'O') & (recData[1] == 'K'))
            return 0;
		else if((recData[0] == 'E') && (recData[1] == 'R'))
			return 1;
		else
			return 1;	
	}
  public long rf_beep(Activity parentActivity, byte time)
  {
	  byte[] sndData = new byte[32];
      byte[] recData = new byte[32];

	 
		///
		sndData[0] = 'A';
		sndData[1] = 'T';
		sndData[2] = 'B';
		sndData[3] = 'B';
		sndData[4] = 'U';
		sndData[5] = time;
		sndData[6] = 0x00;
		sndData[7] = 0x05;
		
		countResult(sndData,(byte)32);

		recData = F9comand( parentActivity,sndData,(byte)32);

		System.arraycopy(recData, 0, sndData, 0, 32);
		countResult(sndData,(byte)32);
	
		if((recData[30]!= sndData[30]) || (recData[31]!= sndData[31]))
			return 3; //У��ʹ���
		if((recData[0] == 'O') & (recData[1] == 'K'))
          return 0;
		else if((recData[0] == 'E') && (recData[1] == 'R'))
			return 1;
		else
			return 1;
  }
}
